﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Ultima;
using Expand;
using System.Windows.Forms;
using System.ComponentModel;
using System.Reflection;
using SpellCaster3.Core.Drawing;

namespace SpellCaster3.Forms.Controls
{
    public class GumpControlButton : GumpControl, IInputOutput
    {
        public GumpControlButton() : base(0)
        {
            Click += new EventHandler(GumpControlButton_Click);
        }

        private void GumpControlButton_Click(object sender, EventArgs e)
        {
            using (GumpPicker dlg = new GumpPicker(Value))
                if (dlg.ShowDialog(this) == DialogResult.OK)
                    Value = dlg.Value;
        }

        public override void Reset()
        {
            Alias = string.Empty;
            KeyName = string.Empty;
            base.Reset();
        }

        public GumpControlButton(int gumpIndex)
            : this()
        {
            Value = gumpIndex;
        }
        public string Alias
        {
            get { return UltimaImage.Alias; }
            set
            {
                UltimaImage.Alias = value;
                Render();
            }
        }

        public string KeyName
        {
            get { return UltimaImage.KeyName; }
            set
            {
                UltimaImage.KeyName = value;
                Render();
            }
        }

        private int _LastValue;

        private int _Value;

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [Browsable(false)]
        [Bindable(true)]
        public int Value
        {
            get
            {
                return _Value;
            }
            set
            {
                _LastValue = Value;
                _Value = value;
                GumpIndex = Value;
                OnValueChanged(this, EventArgs.Empty);
                OnIOValueChanged(this, new LastValueEventArgs<object>(_LastValue));
            }
        }

        protected void OnValueChanged(object sender, EventArgs e)
        {
            if (ValueChanged != null) ValueChanged(sender, e);
        }

        public event EventHandler ValueChanged;

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [Browsable(false)]
        public object IOValue
        {
            get { return (object)Value; }
            set
            {
                Value = (int)value;
            }
        }

        private void OnIOValueChanged(object sender, LastValueEventArgs<object> e)
        {
            if (IOValueChanged != null) IOValueChanged(this, e);
        }

        public event EventHandler<LastValueEventArgs<object>> IOValueChanged;

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // GumpControlButton
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "GumpControlButton";
            this.ResumeLayout(false);
        }
    }
}
